import allure

from common.logging_use import Logger
from config import ASSERTION_LOG


class Assertion:
    # 初始化日志对象
    logger = Logger.init_log_config('assertion', ASSERTION_LOG)

    @staticmethod
    def assert_equal(actual, expected, identifier, message=""):
        """
        断言两个值相等
        :param actual: 实际值
        :param expected: 期望值
        :param message: 断言失败时的提示信息
        """
        try:
            assert actual == expected, f"\n实际值: {actual}，期望值: {expected}，{message}"
            Assertion.logger.info(f"断言成功: {actual} == {expected}，对应的业务场景{identifier}")
            # Allure 中添加断言成功的步骤
            # allure.attach(f"对应的业务场景{identifier}，实际值: {actual}, 期望值: {expected}", "断言成功，", allure.attachment_type.TEXT)
        except AssertionError as e:
            Assertion.logger.error(f"断言失败: {str(e)}")
            # Allure 中添加断言失败的步骤
            # allure.attach(f"断言失败: {str(e)}", "断言失败", allure.attachment_type.TEXT)
            raise

    @staticmethod
    def assert_in(member, container, identifier, message=""):
        """
        断言成员在容器中
        :param member: 成员
        :param container: 容器
        :param message: 断言失败时的提示信息
        """
        try:
            assert member in container, f"期望 {member} 在 {container} 中{message}"
            Assertion.logger.info(f"断言成功: {member} in {container}，对应的业务场景{identifier}")
        except AssertionError as e:
            Assertion.logger.error(f"断言失败: {str(e)}，对应的业务场景{identifier}")
            raise

    @staticmethod
    def assert_true(expr, identifier, message=""):
        """
        断言表达式为True
        :param expr: 表达式
        :param message: 断言失败时的提示信息
        """
        try:
            assert expr, f"期望表达式为True\n{message}"
            Assertion.logger.info(f"断言成功: 表达式为真,对应的业务场景为:{identifier}")
        except AssertionError as e:
            Assertion.logger.error(f"断言失败: {str(e)}，对应的业务场景{identifier}")
            raise

    @staticmethod
    def assert_false(expr, identifier, message=""):
        """
        断言表达式为False
        :param expr: 表达式
        :param message: 断言失败时的提示信息
        """
        try:
            assert not expr, f"期望表达式为False\n{message}"
            Assertion.logger.info(f"断言成功: 表达式为假，对应的业务场景{identifier}")
        except AssertionError as e:
            Assertion.logger.error(f"断言失败: {str(e)}，对应的业务场景{identifier}")
            raise
